#coding:utf-8
import requests
from parsel import Selector
import js2xml
from threading import Thread
n=10
class MyHuaban:
    def __init__(self):
        self.n=n # 需要多少个线程来分别下载图片
        self.url='http://huaban.com/favorite/beauty/'
        self.params={

            'max':'',
            'limit':'100',

        }
        self.headers={
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'
        }
        self.headers['Accept']='application/json'
        self.headers['X-Request']='JSON'
        self.headers['X-Requested-With']='XMLHttpRequest'




    def getpins(self):
        pins_list = []
        while True:
            try:
                z = requests.get(self.url, params=self.params, headers=self.headers)
                for x in z.json()['pins']:
                    # print x['pin_id']
                    pins_list.append(str(x['pin_id']))
                last_pin_id = z.json()['pins'][-1]['pin_id']
                self.params['max'] = last_pin_id
                print 'last_pin_id ', last_pin_id
                if len(pins_list) == 1000:

                    return pins_list

            except:
                break
        return pins_list
#z=requests.get(url=url,params=params,headers=headers)
    def getimgsrc(self,pin_id):
        list = []
        url = "http://huaban.com/pins/" + pin_id

        r = requests.get(url, headers=self.headers)
        for i in r.json()['pin']['board']['pins']:
            # print i['file']['key']
            src = "http://img.hb.aicdn.com/"
            src += i['file']['key']
            list.append(src)
        return list

    def getimgsrc1(self,pin_id):
        list = []
        url = "http://huaban.com/pins/" + pin_id
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36'

        }
        r = requests.get(url, headers=headers)

        sel = Selector(text=r.text)
        print sel
        jscode = sel.xpath("//script[contains(., 'app.page = app.page')]/text()").extract_first()
        js = js2xml.parse(jscode)

        for i in js.xpath('//property[@name="pins"]//property[@name="key"]/string/text()'):
            # print i['file']['key']
            src = "http://img.hb.aicdn.com/"
            src += i
            list.append(src)

        return list

    def downloadimg(self,listurl):
        for url in listurl:
            r = requests.get(url)
            imgname = url.split('/')[-1]
            with open('d:/huaban/%s.jpg' % imgname, 'wb')as img:
                img.write(r.content)


    def getMM(self):
        # print len(getpins())
        # print downloadimg(getimgsrc1('1025893354'))
        # print downloadimg(getimgsrc('1097698786'))

        data = self.getpins()
        for id in data:
            self.downloadimg(self.getimgsrc(str(id)))

class MyThread(Thread):
    def __init__(self,huaban,data):
        Thread.__init__(self)
        self.huaban=huaban
        self.data=data

    def run(self):
        #print Thread.getName(),' start download ',data
        for id in self.data:
            self.huaban.downloadimg(self.huaban.getimgsrc(id))



def df(l, n):
    lenth = (len(l) / n if len(l) % n == 0 else len(l) / n + 1)
    return [l[m:m + lenth] for m in range(0, len(l), lenth)]

def main():
    mm = MyHuaban()
    pinlist = mm.getpins()
    num = 10  # 线程数，等分任务
    task = df(pinlist, num)
    for i in task:
        t = MyThread(MyHuaban(), i)
        t.start()



if __name__=='__main__':
    main()

